package com.qc31.monitorlib.ui.video

import android.content.Context
import android.media.AudioManager
import android.util.AttributeSet
import com.qc31.monitorlib.ui.video.observer.NeedMutesObserver
import com.qc31.monitorlib.ui.video.observer.NeedMutesUtil
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import com.shuyu.gsyvideoplayer.video.base.GSYVideoViewBridge

class AudioPlayer : StandardGSYVideoPlayer, NeedMutesObserver {
    //是否需要静音
    private var isNeedMute = true

    constructor(context: Context?, fullFlag: Boolean?) : super(context, fullFlag)
    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

    override fun changeNeedMutes(isNeedMutes: Boolean) {
        if (isNeedMutes) {
            callback?.finishTalk()
        }
        RealVideoManager.getCustomManager(getKey()).isNeedMute = isNeedMutes
    }

    fun setMute(isNeedMute: Boolean) {
        this.isNeedMute = isNeedMute
        NeedMutesUtil.instance.setAudioState(getKey(), isNeedMute)
    }

    /** Attach 绑定声音和选中监听 */
    fun attachObserver() {
        NeedMutesUtil.instance.attachObserver(getKey(), this)
        RealVideoManager.getCustomManager(getKey()).isNeedMute = isNeedMute
    }

    fun getKey(): String {
        return "audio_key_player$mPlayPosition"
    }

    override fun init(context: Context?) {
        super.init(context)
        isRotateViewAuto = true //是否开启制动旋转
        setIsTouchWigetFull(false)
        isLockLand = true
        onAudioFocusChangeListener = AudioManager.OnAudioFocusChangeListener { focusChange: Int ->
            when (focusChange) {
                AudioManager.AUDIOFOCUS_GAIN -> {
                }
                AudioManager.AUDIOFOCUS_LOSS -> {
                }
                AudioManager.AUDIOFOCUS_LOSS_TRANSIENT -> {
                }
                AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> {
                }
            }
        }
        setVideoAllCallBack(object : SimpleVideoCallBack {
            override fun onPrepared(url: String?, vararg objects: Any?) {
                super.onPrepared(url, *objects)
                callback?.success()
            }

            override fun onPlayError(url: String?, vararg objects: Any?) {
                super.onPlayError(url, *objects)
                callback?.failure()
            }
        })
    }

    override fun getGSYVideoManager(): GSYVideoViewBridge? {
        RealVideoManager.getCustomManager(getKey()).initContext(context.applicationContext)
        return RealVideoManager.getCustomManager(getKey())
    }

    override fun backFromFull(context: Context): Boolean {
        return RealVideoManager.backFromWindowFull(context, getKey())
    }

    public override fun releaseVideos() {
        NeedMutesUtil.instance.detachObserver(getKey())
        RealVideoManager.releaseAllVideos(getKey())
    }


    private var callback: AudioCallBack? = null
    fun setAudioCallBack(back: AudioCallBack) {
        callback = back
    }

    interface AudioCallBack {
        fun finishTalk()
        fun success()
        fun failure()
    }
}